package com.yuwei.mine

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Switch
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.yuwei.common.entity.ServiceEntityItem
import com.yuwei.common.path.RouterPath
import com.yuwei.mine.adapter.MineAdapter

@Route(path = RouterPath.MODULE_MINE_FRAGMENT)
class MineFragment : Fragment() {
    lateinit var adapter: MineAdapter
    lateinit var list: List<ServiceEntityItem>

    @RequiresApi(Build.VERSION_CODES.M)
    @SuppressLint("MissingInflatedId")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view:View=inflater.inflate(R.layout.fragment_mine, container, false)
        val mineRv: RecyclerView = view.findViewById<RecyclerView>(R.id.mine_rv)
        val mineSwitch:Switch = view.findViewById<Switch>(R.id.mine_switch)
        val mineSwitch2:Switch = view.findViewById<Switch>(R.id.mine_switch2)
        val mineSwitch3:Switch = view.findViewById<Switch>(R.id.mine_switch3)
        list = listOf(
            ServiceEntityItem(R.drawable.img_18,"访客管理"),
            ServiceEntityItem(R.drawable.img_19,"物业服务"),
            ServiceEntityItem(R.drawable.img_20,"社区活动"),
            ServiceEntityItem(R.drawable.img_21,"设施预约"),
            ServiceEntityItem(R.drawable.img_22,"生活服务"),
            ServiceEntityItem(R.drawable.img_23,"门禁管理")
        )
        adapter = MineAdapter()
        mineRv.adapter=adapter
        mineRv.layoutManager= GridLayoutManager(requireContext(),4)
        adapter.data.addAll(list)
        adapter.notifyDataSetChanged()
        mineSwitch.setOnCheckedChangeListener { _, isChecked ->
            val thumbColor = if (isChecked) R.color.blue else R.color.teal_11
            val trackColor = if (isChecked) R.color.blue else R.color.teal_11

            mineSwitch.thumbTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, thumbColor))
            mineSwitch.trackTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, trackColor))
        }
        mineSwitch2.setOnCheckedChangeListener { _, isChecked ->
            val thumbColor = if (isChecked) R.color.blue else R.color.teal_11
            val trackColor = if (isChecked) R.color.blue else R.color.teal_11

            mineSwitch2.thumbTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, thumbColor))
            mineSwitch2.trackTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, trackColor))
        }
        mineSwitch3.setOnCheckedChangeListener { _, isChecked ->
            val thumbColor = if (isChecked) R.color.blue else R.color.teal_11
            val trackColor = if (isChecked) R.color.blue else R.color.teal_11

            mineSwitch3.thumbTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, thumbColor))
            mineSwitch3.trackTintList = ColorStateList.valueOf(ContextCompat.getColor(view.context, trackColor))
        }
        return view
    }

}